import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    name:'Main',
    component:()=>import('../views/Main'),
    redirect:'/Home',
    children:[
      {
        path: 'home',
        name: 'Home',
        component: ()=>import('../views/Home'),
      },

    ]
  },
  {
    path: '/plane',
    name: 'Plane',
    component: ()=>import('../views/Plane'),
  },
  {
    path:'/login',
    name:'login',
    component: ()=>import('../views/login')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  const nextRoute = [ 'Plane' ];
  if (nextRoute.indexOf(to.name) >= 0) {
    axios.get("/user/checktoken", {
      params: {token: localStorage.getItem("token")}
    }).then((res) => {
      res = res.data
      if (res.code != 200) {
        router.push({name:'login'})
      }
    })
  }
  next();
});

export default router
